package org.cy.shixun.controller;


import org.cy.shixun.dto.OrderDto;
import org.cy.shixun.entity.Orders;
import org.cy.shixun.service.OrderService;
import org.cy.shixun.utils.RedisUtil;
import org.cy.shixun.vo.OrderVo;
import org.cy.shixun.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;


/**
 * 类名称：OrderController
 * 描述：订单控制器
 *
 * @author: cy
 * @data: 2023/06/29
 */
@RestController
@CrossOrigin
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private OrderService orderService;
    @PostMapping("insert")
    public R orderInsert(@RequestBody OrderDto orderDto, HttpServletRequest request){
        String token=request.getHeader("tk");
        String account = redisUtil.getString(token);
        //判断商品购买数量和库存关系
        orderService.deleteGoods(account);
        Orders orders = orderService.orderInsert(orderDto, account);

        OrderVo orderVo = new OrderVo();
        orderVo.setId(orders.getId());
        orderVo.setPayAmount(orders.getPayAmount());

        return R.success(orderVo);
    }


    @PostMapping("buy")
    public R buy(@RequestBody OrderDto orderDto,HttpServletRequest request){
        String token=request.getHeader("tk");
        String account = redisUtil.getString(token);
        Orders orders = orderService.buy(orderDto, account);
        OrderVo orderVo = new OrderVo();
        orderVo.setId(orders.getId());
        orderVo.setPayAmount(orders.getPayAmount());
        return R.success(orderVo);
    }
}

